package com.focusai.efairy.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;

import com.focusai.efairy.R;
import com.focusai.efairy.model.report.DevLineData;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.widget.LineMarkerView;
import com.focusai.efairy.utils.DateUtils;
import com.focusai.efairy.utils.PrUtils;
import com.focusai.efairy.utils.log.Log;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.List;

/**
 * Created by luo_guangwen on 2017/8/15.
 */

public class DevDetailChartAdapter extends BaseRecyclerAdapter<DevLineData> {
    public static final String TAG = DevDetailChartAdapter.class.getName();
    private int xCountSize = 10;
    public DevDetailChartAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_dev_detail_chart;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder baseRecyclerViewHolder = super.onCreateViewHolder(parent, viewType);
        initLineChart((LineChart) baseRecyclerViewHolder.getView(R.id.dev_line_chart));
        return baseRecyclerViewHolder;
    }

    @Override
    protected void onBindBaseRecyclerViewHolder(BaseRecyclerViewHolder holder, int position) {
//        LineData item = mList.get(position);
        LineChart chart = (LineChart) holder.getView(R.id.dev_line_chart);
        final DevLineData devLineData = mList.get(position);
        holder.setText(R.id.text_name, PrUtils.getNameByPt(devLineData.getPt()));
        chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Long times = devLineData.getxValueDateList().get((int) value);
                String formatDate = DateUtils.getMMDDHHMM(times*1000L);
                return formatDate;
            }
        });
        chart.setData(devLineData.getLineData());
        chart.invalidate();
    }

    private void initLineChart(LineChart lineChart) {
        lineChart.getDescription().setEnabled(false);
        lineChart.setDrawGridBackground(true);
        lineChart.setGridBackgroundColor(mContext.getResources().getColor(R.color.color_line_chart_bg));
        lineChart.getLegend().setEnabled(false);

        //不允许缩放
        lineChart.setDoubleTapToZoomEnabled(false);
        lineChart.setPinchZoom(false);
        lineChart.setScaleEnabled(false);

        //设置点击选中后的提示框
        LineMarkerView markerView = new LineMarkerView(mContext, R.layout.view_line_chart_marker);
        markerView.setChartView(lineChart);
        lineChart.setMarker(markerView);

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setLabelRotationAngle(-60.0f);
        xAxis.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(true);
        xAxis.setLabelCount(xCountSize);
        xAxis.setGridColor(mContext.getResources().getColor(R.color.color_line_grid));


        LimitLine limitLine = new LimitLine(-5f);
        limitLine.setLineColor(mContext.getResources().getColor(R.color.transparent));

        YAxis leftAxis = lineChart.getAxisLeft();
        leftAxis.setLabelCount(4, false);
//        leftAxis.setAxisMinimum(-5f); // this replaces setStartAtZero(true)
        leftAxis.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        leftAxis.addLimitLine(limitLine);

        leftAxis.setDrawAxisLine(false);
        leftAxis.setDrawGridLines(true);
        leftAxis.setGridColor(mContext.getResources().getColor(R.color.color_line_grid));
        leftAxis.enableGridDashedLine(5f, 5f, 0f);

        lineChart.getAxisRight().setEnabled(false);//右边的轴不显示

    }
//    private List<String> getXValueStr(List<Long> dateList){
//        int size = dateList.size();
//        if (size > xCountSize){
//            int length = size/xCountSize;
//            for (int i = 0;i<size;i+=length){
//
//            }
//        }
//    }
}

